<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class HomeController extends Controller
{
   
    //网站首页

    public function index(Request $request)
    {	
    	//判断用户是否登录
        $id=session('userid');
    	//查表传用户昵称到首页
    	$res = DB::table('userdetail')->where('id',$id)->first();
    	//dd($res);
        
        //板块信息
        $ptype=DB::table('portaltype')->where('pid','0') -> skip(0) -> take(7) ->get();
        $lasttype = DB::table('portaltype') -> where('pid','0') -> skip(7) -> take(7) ->get();
         //dd(session()->all());
        $stype=DB::table('portaltype')->where('pid','!=','0')->get();

    	$count=1;

    	//获取友请链接信息
    	$friend = DB::table('friend') -> get();

    	//品种信息
    	$first = DB::table('varieties') -> select(DB::raw('first')) -> groupBy('first') -> get();
        // dd($first);
        //将first转换为数组
        foreach($first as $k => $v){
            $dd[] = $v -> first;
        }

        //获取轮播图
        $fcon=DB::table('fcon')
                ->join('portalpost',function($join){
                    $join->on('fcon.pid','=','portalpost.id')
                        ->where('fcon.position','=','2');
                    })
                ->select('fcon.*','portalpost.title')
                ->get();

        // dd($dd);
        //将所有的品名压入到数组中
        foreach($dd as $k => $v){
            $name[] = DB::table('varieties') -> select(DB::raw('name')) -> where('first',$v) -> get();
        }
        // dd($name);
        $size = '';
        //改为关联数组,下标是首字母
        foreach($dd as $k=>$v){
            for($t=0;$t<count($name[$k]);$t++){
                $size++;
                $data[$v][$t]=$name[$k][$t]->name;
            }
        }
        // dd($data);
        //获取首页上部信息
        $top = $this -> top();
        // dd($top);

        //获取首页中部信息
        $middle = $this -> middle();
        $m1 = 1;
        $m2 = 1;
        // dd($middle);

        //获取首页下部信息
        $bottom = $this -> bottom();
        // dd($bottom);

        //获取底部信息
        $low = $this -> low();
    	return view('Home.home.index',['res' => $res,'friends' => $friend,'first' => $first,'fcon' => $fcon,
            'top' => $top,'middle' => $middle,'m1' => $m1,'m2' => $m2,'bottom' => $bottom,'low' => $low,
            'name' => $name,'ptype'=>$ptype,'ltype' => $lasttype,
            'stype'=>$stype,'count'=>$count]);

    }

    //首页上部信息
    public function top()
    {
        //类别名信息
        // $tname = 
        //获取热点信息
        $hot = DB::table('portalpost')
            -> join('portaltype','portalpost.tid','=','portaltype.id')
            -> select('portalpost.id','portalpost.title','portaltype.id as tid','portaltype.name')
            -> whereIn('tid',[55,56,57,58])
            -> where('hot','1')
            -> orWhere('title','like','%涨价%')
            -> orWhere('title','like','%头条%')
            -> orderBy('ctime','desc')
            -> skip(0) -> take(14) -> get();
        
        //获取资讯信息
        $time = DB::table('portalpost')
            -> join('portaltype','portalpost.tid','=','portaltype.id')
            -> select('portalpost.id','portalpost.title','portaltype.id as tid','portaltype.name')
            -> orderBy('ctime','desc')
            -> skip(0) -> take(7) -> get();

        //价格涨幅榜
        $priceup = DB::table('mprice')
            -> orderBy('month','desc')
            -> skip(0) -> take(6) -> get();
        $pricedown = DB::table('mprice')
            -> orderBy('month','asc')
            -> skip(0) -> take(6) -> get();

        //成交榜
        $sales = DB::table('medicinal')
            -> orderBy('sales','desc')
            -> skip(0) -> take(14) -> get();
        
        $top[] = $hot;
        $top[] = $time;
        $top[] = $priceup;
        $top[] = $pricedown;
        $top[] = $sales;
        
        return $top;

    }       

    //首页中部信息
    public function middle()
    {
        $info = DB::table('portalpost')
            -> join('portaltype','portalpost.tid','=','portaltype.id')
            -> select('portalpost.id','portalpost.title','portalpost.tid','portaltype.name')
            -> where('portaltype.path','0-41')
            // -> groupBy('portaltype.name')
            -> orderBy('ctime','desc')
            // -> skip(0) -> take(6) 
            -> get();
        $type = DB::table('portaltype')
            -> select('id','name')
            -> where('path','0-41')
            ->get();
       
       $middle[] = $info;
       $middle[] = $type;

        return $middle;
    }

    //首页下部信息
    public function bottom()
    {
        //药材评测信息
        $pingce = DB::table('portalpost')
            -> join('portaltype','portalpost.tid','=','portaltype.id')
            -> select('portalpost.id','portalpost.title','portalpost.tid')
            -> where('portaltype.name','=','评测')
            -> orderBy('ctime','desc')
            ->skip(0) -> take(8) -> get();

        //药材板块信息
        $mtype = DB::table('portaltype') -> select('id','name') -> where('pid','42') -> get();
        
        //药材信息详情
        $mpost = DB::table('portalpost')
            -> join('portaltype','portalpost.tid','=','portaltype.id')
            -> select('portalpost.id','portalpost.title','portalpost.img','portalpost.tid')
            -> where('portaltype.pid','42')
            -> orderBy('ctime','desc')
            -> get();

        //标准信息
        $biaozhun = DB::table('portalpost')
            -> join('portaltype','portalpost.tid','=','portaltype.id')
            -> select('portalpost.id','portalpost.title','portalpost.tid')
            -> where('portaltype.name','=','天地网标准')
            -> orderBy('ctime','desc')
            ->skip(0) -> take(7) -> get();

        //真假板块

        //真假存储
        $save = DB::table('portalpost')
            -> join('portaltype','portalpost.tid','=','portaltype.id')
            -> select('portalpost.id','portalpost.title','portalpost.tid','portalpost.img','portaltype.name')
            -> where('portaltype.name','=','真伪')
            -> orWhere('portaltype.name','=','存储')
            -> orderBy('ctime','desc')
            -> get();

        $bottom[] = $pingce;
        $bottom[] = $mtype;
        $bottom[] = $mpost;
        $bottom[] = $biaozhun;
        $bottom[] = $save;
        return $bottom;
    }

    //首页底部信息
    public function low()
    {
        //人气品种
        $varieties = DB::table('varieties') -> orderBy('stimes','desc') -> skip(0) -> take(30) -> get();
        //获取热销商品信息
        $sales = DB::table('medicinal')
            // -> select('id','name','mimg')
            -> orderBy('sales','desc')
            -> skip(0) -> take(10) -> get();

        $low[] = $varieties;
        $low[] = $sales;

        return $low;
    }   

    //搜索
    public function postSearch(Request $request)
    {
        // $data = $request -> only(['search']);
        // $search = $data['search'];
        $search = $request -> input('search');
        // dd($search);
        //判断是否有搜索
        if($search == ''){
            // dd(222);
            return back();
        }else{
            //获取药材品种名
            $mm = DB::table('varieties') -> select('name') -> get();
            foreach ($mm as $key => $value) {
                $names[] = $value -> name;
            }
            // dd($name);
            //更改数据库的搜索次数
            if(in_array($search,$names)){
                $name = DB::table('varieties') -> select('stimes') -> where('name',$search) -> first();
                $times = $name -> stimes + 1;
                // dd($times);
                $stimes['stimes'] = $times;
                $res = DB::table('varieties') -> where('name',$search) -> update($stimes);
                // dd($res);
            }

            $res = DB::table('varieties') 
                ->select('name')
                -> where('name','like','%'.$search.'%') 
                ->orWhere('pinyin','like','%'.$search.'%')
                ->skip(0) -> take(5) -> get();
            // dd($res);
            $data = array();
            foreach ($res as $key => $value) {
                foreach ($value as $k => $v) {
                    $data[] = $v;
                    // dd($v);
                }
            }

            //价格
            $price = DB::table('mprice')
                -> where('mName',$search)
                -> orderBy('time','desc')
                -> groupBy('market')
                -> skip(0) -> take(4) -> get();
            // dd($price);

            //帖子,新闻信息
            $num = DB::table('portalpost')
                -> join('portaltype','portalpost.tid','=','portaltype.id')
                -> select(DB::raw('portaltype.name,count(tid) as ff'))
                -> where('portalpost.title','like','%'.$search.'%')
                -> groupBy('tid')
                -> get();
            // dd($num);
            $ncounts = DB::table('portalpost') 
                -> select(DB::raw('count(*) as num'))
                -> where('title','like','%'.$search.'%') 
                ->get();
            $nnum = $ncounts[0] -> num;
            // dd($num);
            $news = DB::table('portalpost')
                -> where('title','like','%'.$search.'%') 
                -> orderBy('ctime','asc') -> skip(0) -> take(5) -> get();
            // dd($news);

            $pcounts = DB::table('post') 
                    -> select(DB::raw('count(*) as counts'))
                    -> where('title','like','%'.$search.'%') 
                    ->get();
            $pnum = $pcounts[0] -> counts;
            $posts = DB::table('post')        
                    -> where('title','like','%'.$search.'%') 
                    -> orderBy('ctime','asc') -> skip(0) -> take(4) -> get();
           
            // dd($posts);
            return view('Home.home.search',['name' => $search,'res' => $data,'prices' => $price,'nnum' => $nnum,'num' => $num,'news' => $news,'pnum' => $pnum,'posts' => $posts]);
        }

    }

    //搜索新闻信息页面
    public function getNews($news=1,$post=2)
    {
               
    	//return view('Home.home.index',['res'=>$res,'hotest'=>$hotest,'fcon'=>$fcon,'friends' => $friend,'first' => $first,'name' => $name,'ptype'=>$ptype,'stype'=>$stype,'count'=>$count]);

        //新闻信息
        if($news != 1){
            $num = DB::table('portalpost')
                -> join('portaltype','portalpost.tid','=','portaltype.id')
                -> select(DB::raw('portaltype.name,count(tid) as ff'))
                -> where('portalpost.title','like','%'.$news.'%')
                -> groupBy('tid')
                -> get();
            // dd($num);
            $ncounts = DB::table('portalpost') 
                -> select(DB::raw('count(*) as num'))
                -> where('title','like','%'.$news.'%') 
                ->get();
            $nnum = $ncounts[0] -> num;
            // dd($num);
            $newss = DB::table('portalpost')
                -> where('title','like','%'.$news.'%') 
                -> orderBy('ctime','asc') -> skip(0) -> take(10) -> get();
            // dd($news);
            return view('Home.home.news',['name' => $news,'num' => $num,'nnum' => $nnum,'news' => $newss]);
        }else{
            //条数
            $pcounts = DB::table('post') 
                -> select(DB::raw('count(*) as counts'))
                -> where('title','like','%'.$post.'%') 
                ->get();
            $pnum = $pcounts[0] -> counts;
            //内容
            $posts = DB::table('post')        
                -> where('title','like','%'.$post.'%') 
                -> orderBy('ctime','asc') -> skip(0) -> take(4) -> get();
           
            // dd($posts);
            return view('Home.home.news',['name' => $post,'pnum' => $pnum,'posts' => $posts]);
        }
    }

    
}
